package com.cuz.daileetcode;

import com.cuz.daileetcode.utils.ArrayUtils;

public class Day26_1 {

    public static void main(String[] args) {
        int[] arr = {2, 2, 1, 6, 5};
        countSort(arr);
        ArrayUtils.print(arr);
    }

    public static void countSort(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        //2,2,1,6,5
        int max = Integer.MIN_VALUE;
        for (int i : arr) {
            max = Math.max(i, max);
        }
        //new int[6]
        int[] countArray = new int[max + 1];
        //记录每一个数的数量
        for (int index = 0; index < arr.length; index++) {
            countArray[arr[index]] += 1;
        }
        int countInArray = 0;
        //countArray=>0 1 2 0 0 1 1=>1 2 2  5 6
        for (int countIndex = 0; countIndex < countArray.length; countIndex++) {
            int count = countArray[countIndex];
            int indexSetBack = 0;
            while (indexSetBack < count) {
                arr[countInArray] = countIndex;
                indexSetBack++;
                countInArray++;
            }
        }
    }
}
